package com.erp.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.erp.CaptureActivity;
import com.erp.contacts.Contacts;
import com.erp.Interface.OnUserDataInterface;
import com.erp.mclass.QuanXian;
import com.erp.module.scanBaomingxi.ScanActivity;
import com.erp.permission.PermissionListener;
import com.erp.R;
import com.erp.util.Httputil;
import com.erp.util.Myutil;
import com.erp.util.PermissionUtils;
import com.erp.view.MyAlerDialog;

import java.util.List;

/**
 * Created by Administrator on 2016/11/9/009.
 * 作者：刘洋
 */
public class DialogActivity extends BasedialogActivity implements View.OnClickListener {
    private TextView tv_quite_confirm,tv_quite_cancel,tv_dialog;
    private LinearLayout ll_quit,ll_dingdan_code,ll_danju_code,ll_tuzhi_code,ll_wuliao_code,ll_biaoqian,ll_BDsaoma,ll_Baomingxi;
    private AlertDialog updatedialog;
    private boolean isscdd;
    private boolean isdanju;
    private boolean isShuangChuanAnZhuang;
    private String a="DialogActivity";
    @Override
    protected int getLayoutId() {
        activityname=a;
        return R.layout.dialog_activity;
    }

    @Override
    protected void initCreate() {
        WindowManager.LayoutParams p = getWindow().getAttributes();  //获取对话框当前的参数值
        p.alpha = 1.0f;      //设置本身透明度
        p.dimAmount = 0.1f;      //设置黑暗度
        getWindow().setAttributes(p);     //设置生效
        getWindow().setGravity(Gravity.RIGHT|Gravity.TOP);       //设置靠右对齐
        IntentFilter filter = new IntentFilter();
        filter.addAction(Contacts.BORADCAST_ACTION_EXIT);//为BroadcastReceiver指定一个action，即要监听的消息名字
        registerReceiver(mBoradcastReceiver,filter); //动态注册监听  静态的话 在AndroidManifest.xml中定义
        initView();
        initonclick();
    }
    private void initonclick() {
        //点击按钮跳转到二维码扫描界面，这里用的是startActivityForResult跳转
        //扫描完了之后调到该界面
        tv_dialog.setOnClickListener(this);
        ll_quit.setOnClickListener(this);
        ll_dingdan_code.setOnClickListener(this);
        ll_danju_code.setOnClickListener(this);
        ll_tuzhi_code.setOnClickListener(this);
        ll_wuliao_code.setOnClickListener(this);
        ll_biaoqian.setOnClickListener(this);
        ll_BDsaoma.setOnClickListener(this);
        ll_Baomingxi.setOnClickListener(this);
    }

    private void saomiao(final int request) {
        PermissionUtils.Companion.issaomiao(DialogActivity.this, new PermissionListener() {
            @Override
            public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                Intent intent = new Intent();
                intent.setClass(DialogActivity.this, CaptureActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivityForResult(intent, request);
            }

            @Override
            public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                Myutil.MyTost("没有权限，无法扫描");
            }
        });
    }

    private void quitdialog() {
        final MyAlerDialog myAlerDialog=new MyAlerDialog(this);
        myAlerDialog.start();
        myAlerDialog.setdata("确定退出登录吗？");
        myAlerDialog.setleft("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myAlerDialog.dismiss();
                updatedialog = Myutil.showupdatedialog(DialogActivity.this, "正在退出登录", true);
                Httputil.setLogout(DialogActivity.this, new OnUserDataInterface() {
                    @Override
                    public void onUserData(String data) {
                        if (data.indexOf("true") != -1) {
                            if(updatedialog!=null) {
                                updatedialog.dismiss();
                            }
                            Myutil.MyTost("退出登录状态成功");
                            Intent intent = new Intent(DialogActivity.this, LoginActivity.class);
                            startActivity(intent);
                            editor.putBoolean("islogin", false);
                            editor.commit();
//                                    finish();
                            Intent intent1 = new Intent();
                            intent1.setAction(Contacts.BORADCAST_ACTION_EXIT);
                            sendBroadcast(intent1);//发送退出系统广播  每个接收器都会收到 调动finish（）关闭
                        }
                    }

                    @Override
                    public void onNot(String throwable) {
                        myAlerDialog.dismiss();
                        if(updatedialog!=null){
                            updatedialog.dismiss();
                        }
                        if (throwable.indexOf("Access denied for user anonymous") != -1 || throwable.indexOf("User is not logged in") != -1) {
                            Intent intent = new Intent(DialogActivity.this, LoginActivity.class);
                            startActivity(intent);
                            Intent intent1 = new Intent();
                            intent1.setAction(Contacts.BORADCAST_ACTION_EXIT);
                            sendBroadcast(intent1);//发送退出系统广播  每个接收器都会收到 调动finish（）关闭
                        }
                        Myutil.MyTost("网络访问失败");
                    }
                });
            }
        });
        myAlerDialog.setright("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myAlerDialog.dismiss();
            }
        });
        myAlerDialog.show();
    }

    private void initView() {
        isscdd=sp.getBoolean("isscdd",false);
        isdanju=sp.getBoolean("isdanju",false);
        isShuangChuanAnZhuang=sp.getBoolean("isShuangChuanAnZhuang",false);
        tv_dialog= (TextView) findViewById(R.id.tv_dialog);
        ll_dingdan_code= (LinearLayout) findViewById(R.id.ll_dingdan_code);
        ll_danju_code= (LinearLayout) findViewById(R.id.ll_danju_code);
        ll_tuzhi_code= (LinearLayout) findViewById(R.id.ll_tuzhi_code);
        ll_wuliao_code= (LinearLayout) findViewById(R.id.ll_wuliao_code);
        ll_quit= (LinearLayout) findViewById(R.id.ll_quit);
        ll_biaoqian= (LinearLayout) findViewById(R.id.ll_biaoqian);
        ll_BDsaoma = (LinearLayout)findViewById(R.id.ll_BDsaoma);
        ll_Baomingxi = findViewById(R.id.ll_Baomingxi);
        if(isscdd==true){
            ll_dingdan_code.setVisibility(View.VISIBLE);
            ll_danju_code.setVisibility(View.VISIBLE);
            ll_tuzhi_code.setVisibility(View.VISIBLE);
            ll_wuliao_code.setVisibility(View.VISIBLE);
        }else{
            ll_dingdan_code.setVisibility(View.GONE);
            ll_danju_code.setVisibility(View.GONE);
            ll_tuzhi_code.setVisibility(View.GONE);
            ll_wuliao_code.setVisibility(View.GONE);
        }
        if(isShuangChuanAnZhuang){
            ll_biaoqian.setVisibility(View.VISIBLE);
        }else{
            ll_biaoqian.setVisibility(View.GONE);
        }
        if(QuanXian.Companion.isManufacturingInformation()){
            ll_BDsaoma.setVisibility(View.VISIBLE);
        }else{
            ll_BDsaoma.setVisibility(View.GONE);
        }
        if(QuanXian.Companion.getIsscdd()){
            ll_Baomingxi.setVisibility(View.VISIBLE);
        }else{
            ll_Baomingxi.setVisibility(View.GONE);
        }
    }
    private BroadcastReceiver mBoradcastReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(Contacts.BORADCAST_ACTION_EXIT)){//发来关闭action的广播
                finish();
            }
        }
    };
    /**
     * onActivityResult方法，回调扫码和拍照获得的数据
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Intent intent=new Intent();
        if(resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String s = (String) bundle.get("result");

            switch (requestCode) {
                case 1:
                    intent = new Intent(DialogActivity.this, HomeActivity.class);
                    intent.putExtra("title", "订单进度");
                    intent.putExtra("code", 70);
                    intent.putExtra("select", 2);
                    intent.putExtra("all", s);
                    startActivity(intent);

//                    intent.putExtra("all", s);
//                    intent.putExtra("select", 2);
//                    setResult(RESULT_OK, intent);
                    finish();
                    break;
                case 2:
                    intent = new Intent(DialogActivity.this, HomeActivity.class);
                    intent.putExtra("title", "订单进度");
                    intent.putExtra("code", 70);
                    intent.putExtra("select", 3);
                    intent.putExtra("all", s);
                    startActivity(intent);

//                    intent.putExtra("all", s);
//                    intent.putExtra("select", 3);
//                    setResult(RESULT_OK, intent);
                    finish();
                    break;
                case 3:
                    intent = new Intent(DialogActivity.this, HomeActivity.class);
                    intent.putExtra("title", "订单进度");
                    intent.putExtra("code", 70);
                    intent.putExtra("select", 4);
                    intent.putExtra("all", s);
                    startActivity(intent);

//                    intent.putExtra("all", s);
//                    intent.putExtra("select", 4);
//                    setResult(RESULT_OK, intent);
                    finish();
                    break;
                case 4:
                    intent = new Intent(DialogActivity.this, HomeActivity.class);
                    intent.putExtra("title", "订单进度");
                    intent.putExtra("code", 70);
                    intent.putExtra("select", 5);
                    intent.putExtra("all", s);
                    startActivity(intent);
//                    intent.putExtra("all", s);
//                    intent.putExtra("select", 5);
//                    setResult(RESULT_OK, intent);
                    finish();
                    break;
                case 5:

                    intent = new Intent(DialogActivity.this, HomeActivity.class);
                    intent.putExtra("title", "项目管理");
                    intent.putExtra("isTiao", true);
                    intent.putExtra("all", s);
                    intent.putExtra("code", 130);
                    startActivity(intent);
//
//                    Bundle bundle = data.getExtras();
//                    String s= (String) bundle.get("result");
//                    intent.putExtra("s",s);
//                    intent.putExtra("select",21);
//                    setResult(RESULT_OK,intent);
                    finish();
                    break;
                case 6:
                    intent = new Intent(DialogActivity.this, GonggaoTextActivity.class);
                    intent.putExtra("url", Contacts.BD_TUPIAN + s + "?mobile=1");
                    intent.putExtra("title", "BD图纸查看");
                    startActivity(intent);

                    finish();
                    break;
                case 7:
                    intent = new Intent(DialogActivity.this, ScanActivity.class);
                    String[] ss = s.split("#");
                    String scan = ss[0];
                    intent.putExtra("scan", scan);
                    startActivity(intent);

                    finish();
                    break;
            }
        }
    }
    /**
     * 回调收到权限请求时已经完成
     */
    @Override
    public void onRequestPermissionsResult(final int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_dialog:
                finish();
                break;
            case R.id.ll_quit:
                quitdialog();
                break;
            case R.id.ll_dingdan_code:
                saomiao(1);
                break;
            case R.id.ll_danju_code:
                saomiao(2);
                break;
            case R.id.ll_tuzhi_code:
                saomiao(3);
                break;
            case R.id.ll_wuliao_code:
                saomiao(4);
                break;
            case R.id.ll_biaoqian:
                saomiao(5);
                break;
            case  R.id.ll_BDsaoma:
                saomiao(6);
                break;
            case R.id.ll_Baomingxi:
                saomiao(7);
                break;
        }
    }
}
